#!/usr/bin/python3
 
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
import datetime

#jiawei.liu4@sfmail.sf-express.com 
td = datetime.datetime.now().strftime('%Y-%m-%d')

def Sendtomail(): 
    # 第三方 SMTP 服务
    mail_host="mail.SFMAIL.SF-EXPRESS.COM"        #设置服务器
    mail_user="JIAWEI.LIU4@SFMAIL.SF-EXPRESS.COM" #用户名
    mail_pass="Wwwljw.com"                        #口令 
     
     
    sender = 'JIAWEI.LIU4@SFMAIL.SF-EXPRESS.COM'
    receivers = ['JIAWEI.LIU4@SFMAIL.SF-EXPRESS.COM']  # 接收邮件，可设置为你的QQ邮箱或者其他邮箱
     
    #message = MIMEText('Python 邮件发送测试-MIMEText...', 'plain', 'utf-8')
    #message['From'] = Header("JIAWEI.LIU4@SFMAIL.SF-EXPRESS.COM", 'utf-8')
    #message['To'] =  Header("JIAWEI.LIU4@SFMAIL.SF-EXPRESS.COM", 'utf-8')
    #subject = ''
    #message['Subject'] = Header(subject, 'utf-8')
    
    
    #创建一个带附件的实例
    message = MIMEMultipart()
    message['From'] = Header("JIAWEI.LIU4@SFMAIL.SF-EXPRESS.COM", 'utf-8')#显示的发件人
    message['To'] =  Header("JIAWEI.LIU4@SFMAIL.SF-EXPRESS.COM", 'utf-8') #显示的收件人
    subject = 'Python-SMTP邮件:TITLE这是今天你的发布任务'
    message['Subject'] = Header(subject, 'utf-8')
     
    #邮件正文内容
    message.attach(MIMEText('这是今天你的发布:15:30点时采集的任务,一份全量,一份自己的', 'plain', 'utf-8')) 
    #构造附件1，传送当前目录下的文件
    att1 = MIMEText(open('youdeploy.json', 'rb').read(), 'base64', 'utf-8')
    att1["Content-Type"] = 'application/octet-stream'
    att1["Content-Disposition"] = 'attachment; filename="youdeploy.txt"'    #这里的filename可以任意写，写什么名字，邮件中显示什么名字
    message.attach(att1)
    #构造附件2
    att2 = MIMEText(open('todaydeploy.json', 'rb').read(), 'base64', 'utf-8') 
    att2["Content-Type"] = 'application/octet-stream'
    att2["Content-Disposition"] = 'attachment; filename="todaydeploy.txt"'    #这里的filename可以任意写，写什么名字，邮件中显示什么名字
    message.attach(att2)
    
    
    try:
        smtpObj = smtplib.SMTP() 
        smtpObj.connect(mail_host, 25)    # 25 为 SMTP 端口号
        smtpObj.login(mail_user,mail_pass)
        smtpObj.sendmail(sender, receivers, message.as_string())
        print ("邮件发送成功")
    except smtplib.SMTPException:
        print ("Error: 无法发送邮件")
		
if __name__ == '__main__':
    Sendtomail()